// JavaScript Document
var params;
var orderInfo;
//var base64 = new Base64();



function CreateOrder(infos)
{
	var params = {"rCode": WMALL000005};
	
	params.orderPayment = 3;
	params.orderChannl = 1;	
	params.infos = infos;//JSON.stringify(infos);
	
	GetRequestData(params, CreateOrderCallback);		
}

function CreateOrderCallback(data)
{
	if (data[JSON_KEY_CODE] == RESPOND_CODE_SUCCESS)
	{
		if (data.precreate == 1)
		{
			callPay(data, WxPayCallback);
		}
		else
		{
			// 请求预下单
			PrecreateOrder(data.orderNo);
		}
		
		// 删除购物车数据
		for (var i=0; i<orderInfo.length; i++)
		{		
			RemoveCart(orderInfo[i].Id);
		}
	}
	else if (data[JSON_KEY_CODE] == RESPOND_CODE_RECORD_NOT_EXIST)
	{
		
	}
	else if (data[JSON_KEY_CODE] == RESPOND_CODE_AJAX_FAILURE)
	{
	}
	else
	{
	}
}

function DisplayOrderInfo(data)
{
	console.log(data.length);
	var _html = '';
	var template = 
			'<div class="order_item">' +
            	'<img src="{0}">' +
                '<div class="order_text">' +
                	'<a class="goods_name">{1}</a>' +
                    '<a class="goods_price">￥ <strong>{2}</strong></a>' +
                    '<a class="goods_count_label">购买数量 <label>x{3}</label></a>' +
                    '<input type="hidden" value="{4}" />' +
                '</div>' +
            '</div>';
	
	var count = 0;
	var amount = 0;
	
	for (var i=0; i<data.length; i++)
	{		
		_html += template.format(data[i].photo, data[i].commodityName, data[i].commodityAmount, data[i].commodityNums, data[i].commodityId);
		
		count += data[i].commodityNums;
		amount += data[i].commodityAmount * data[i].commodityNums;
	}
	
	$('#order_detail').html(_html);
	
	$('#pay_count').find('strong').html(count);
	$('#pay_amount').find('strong').html(amount.toFixed(2));
}

function CommitOrder()
{
	var infos = [];
	
	for (var i=0; i<orderInfo.length; i++)
	{		
		var _item = {};
		
		_item.gdsNo = orderInfo[i].Id;
		_item.gdsType = 1;
		_item.gdsNum = orderInfo[i].Count;
		
		infos.push(_item);
	}
	
	CreateOrder(infos);
}

/*function PrecreateOrder(orderNO)
{
	var params = {"rCode": WMALL000006};
	
	params.orderNo = orderNO;//JSON.stringify(infos);
	
	GetRequestData(params, PrecreateOrderCallback);		
}

function PrecreateOrderCallback(data)
{
	if (data[JSON_KEY_CODE] == RESPOND_CODE_SUCCESS)
	{
		callPay(data, WxPayCallback);
	}
	else if (data[JSON_KEY_CODE] == RESPOND_CODE_RECORD_NOT_EXIST)
	{
		
	}
	else if (data[JSON_KEY_CODE] == RESPOND_CODE_AJAX_FAILURE)
	{
	}
	else
	{
	}
}

function WxPayCallback(data)
{
	var payResult = 0;
	
	//alert('state ' + data.state)
	
	if (data.state == 'ok')
	{
		payResult = 1;
	}
	else if (data.state == 'fail')
	{
		payResult = 0; 
	}
	else if (data.state == 'cancel')
	{
		payResult = 2; 
	}
		
	var payInfo = {};
	payInfo.NO = data.orderNo;
	payInfo.PayResult = payResult;
	payInfo.Amount = parseFloat($('#pay_amount').find('strong').html());
	
	//var payInfoBase64 = base64.encode(JSON.stringify(payInfo));
	
	GotoResult(payInfo);
}*/

